define([
	'ui',
	'chessman'
],function(UI,Chessman){
	'use strict';
	
	var Game = function(level){
		/**
		 * 属性声明
		 */
		this.$ui = null;		//UI界面
		this.players = new Array();     //玩家
		this.steps = new Array();       //落子记录
		this.steps.push(new Array());   //黑棋
		this.steps.push(new Array());   //白棋
		this.turn = null; 		//落子人
		this.winner = null;             //赢家
		this.level = level;      //电脑AI等级

		initGame(this);
	}
	//初始化游戏程序
	function initGame($this){
		initUI($this);
		initPlayers($this);
	}
	//初始化UI界面
	function initUI($this){
		$this.$ui = UI.create();
	}
	//初始化玩家
	function initPlayers($this){
		
		var Player = function(color,isAI,level){
			this.color = color;
			this.isAI = isAI;
			this.level = level;
		}
		$this.players.push(new Player(Chessman.BLACK,false,0));
		$this.players.push(new Player(Chessman.WHITE,false,0));

		$this.turn = $this.players[Chessman.BLACK];
		$this.$ui.setTurn($this.turn.color);
	}
	
	function doTurn($this){
		$this.turn = $this.players[Chessman.getOther($this.turn.color)];
		$this.$ui.setTurn($this.turn.color);
	}

    Game.prototype = {
		start: function(){
			var $this = this;
			document.body.addEventListener('click',function(e){
				if(e.target.className == 'clickCell'){
					var id = e.target.id.split(',');
					var position = {
						x : id[0],
						y : id[1]
					}
					$this.$ui.downChessman(position,$this.turn.color);
					doTurn($this);
				}else{
					e.returnValue = false;
				}
			});
			$this.$ui.controlPanel.addEventListener('click',function(e){
				if(/setting/g.test(e.target.className)){
					$this.$ui.alert(e.target,"设置",function(ele){
						console.log("点击了确定");
						ele.style.display = 'none';
						
					});
				}
			});
		}
	}

    return Game;
})
